﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Html5Css3.Models;
using System.Net;
using System.Xml.Linq;
using MvcApplicationWSDL.Content;
namespace Html5Css3.Controllers
{
    public class WSDLController : Controller
    {
        //
        // GET: /WSDL/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /WSDL/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /WSDL/Create

        public ActionResult Create()
        {
                WsdlModel m = new WsdlModel();
                m.Id = -1;
                m.Domain = "localhost";
                m.Password = "pass";
                m.Port = 80;
                m.ProxyServer = "localhost";
                m.Url = "http://www.webservicex.net/whois.asmx?WSDL";
                m.UserName = "username";
                m.Wsdl = "content";
                // TODO: Add insert logic here
                return View(m);
     
        } 

        //
        // POST: /WSDL/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                ValueProviderResult url = collection.GetValue("txtUrl");

                WebClient c = new WebClient();
                string wsdl =  c.DownloadString(url.AttemptedValue);


                WsdlModel m = new WsdlModel();
                m.Id = 8;
                m.Domain = "localhost";
                m.Password = "pass";
                m.Port = 80;
                m.ProxyServer = "localhost";
                m.Url = url.AttemptedValue;
                m.UserName = "username";
                m.Wsdl = wsdl;// "todo at " + DateTime.Now.ToLongDateString();
               // ViewData["wsdl"] = m;
               //

                dynamic xml = XElement.Parse(wsdl, LoadOptions.SetBaseUri).ToElastic();
                m.UserName = xml.definitions.service.name;

                return View(m);

            }
            catch(Exception es)
            {
                return View();
            }
        }
        
        //
        // GET: /WSDL/Edit/5
 
        public ActionResult Edit(int id)
        {

            WsdlModel m = new WsdlModel();
            m.Id = 8;
            m.Domain = "localhost";
            m.Password = "pass";
            m.Port = 80;
            m.ProxyServer = "localhost";
            m.Url = "enter it..";
            m.UserName = "username";
            m.Wsdl = "todo at " + DateTime.Now.ToLongDateString();

            m = (WsdlModel)ViewData["wsdl"];
            return View(m);
        }

        //
        // POST: /WSDL/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /WSDL/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /WSDL/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
